package com.choosefine.paycenter.webgateway.pay.web.controller;

import com.choosefine.paycenter.account.dto.RechargeDto;
import com.choosefine.paycenter.common.dto.JSONResult;
import com.choosefine.paycenter.common.utils.JSONResultGenerator;
import com.choosefine.paycenter.pay.api.RechargeService;
import com.choosefine.paycenter.pay.api.dispatcher.RechargeDispatcher;
import com.choosefine.paycenter.pay.api.handler.RechargeHandler;
import com.choosefine.paycenter.pay.vo.PayResultVo;
import com.choosefine.paycenter.pay.vo.RechargeQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Comments：
 * Author：Jay Chang
 * Create Date：2017/3/27
 * Modified By：
 * Modified Date：
 * Why & What is modified：
 * Version：v1.0
 */
@Api(value = "充值",description = "张洁")
@RestController
@RequestMapping("account/recharge")
public class RechargeController {
    @Autowired
    private RechargeDispatcher rechargeDispatcher;

    @Autowired
    private RechargeService rechargeService;

    @ApiOperation("充值操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode",dataType = "String",paramType = "header",required = true,value = "租户编码"),
            @ApiImplicitParam(name = "amount",dataType = "BigDecimal",paramType = "body",required = true,value = "金额"),
            @ApiImplicitParam(name = "returnUrl",dataType = "String",paramType = "body",value = "银行页面回调url"),
            @ApiImplicitParam(name = "operName",dataType = "String",paramType = "body",required = true,value = "操作员"),
            @ApiImplicitParam(name = "channel",dataType = "String",allowableValues = "CCB,ABC",paramType = "body",required = true,value = "支付通道(即哪个银行)"),
            @ApiImplicitParam(name = "payType",dataType = "String",allowableValues = "B2C_NB,B2C_APP,B2B_NB",paramType = "body",required = true,value = "支付类型"),
    })
    @PostMapping
    public JSONResult doRecharge(@RequestHeader("userCode") @NotBlank String userCode, @Validated @RequestBody RechargeDto rechargeDto){
        rechargeDto.setUserCode(userCode);
        RechargeHandler rechargeHandler = rechargeDispatcher.getRechargeHandler(rechargeDto.getChannel());
        PayResultVo payResultVo = rechargeHandler.doProcess(rechargeDto);
        return JSONResultGenerator.buildOperSuccessResult(payResultVo);
    }

    @ApiOperation("根据充值流水号查询充值是否成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rSn",dataType = "String",paramType = "query",required = true,value = "充值流水号"),
    })
    @GetMapping("status/rSn")
    @ResponseBody
    public JSONResult findPaymentStatus(@NotBlank String rSn){
        RechargeQueryVo rechargeQueryVo = rechargeService.findRechargeQueryResultByRSn(rSn);
        return JSONResultGenerator.buildQuerySuccessResult(rechargeQueryVo);
    }
}
